package com.gxc;

/**
 * @author 宫新程
 * @since 2018/12/15 10:41 AM
 */
public class Test_String {

  /** 得到cookie的域名 */
  public static void main(String[] args) {

    String domainName = null;
    String serverName = "http://www.baidu.com/admin.html";
    if (serverName == null || serverName.equals("")) {
      domainName = "";
    } else {
      serverName = serverName.toLowerCase();
      serverName = serverName.substring(7); //    39.105.25.213:8082/
      final int end = serverName.indexOf("/");
      serverName = serverName.substring(0, end); // 39.105.25.213:8082
      final String[] domains = serverName.split("\\.");
      int len = domains.length;
      if (len > 3) {
        // 1.www.xxx.com.cn
        // domainName = "." + domains[len - 3] + "." + domains[len - 2] + "." + domains[len - 1];
        // 2.http://39.105.25.213:8085
        domainName =
            domains[len - 4]
                + "."
                + domains[len - 3]
                + "."
                + domains[len - 2]
                + "."
                + domains[len - 1];
      } else if (len <= 3 && len > 1) {
        // xxx.com or xxx.cn
        domainName = "." + domains[len - 2] + "." + domains[len - 1];
      } else {
        domainName = serverName;
      }
    }

    if (domainName != null && domainName.indexOf(":") > 0) {
      String[] ary = domainName.split("\\:");
      domainName = ary[0];
    }

    System.out.println(domainName);
  }
}
